﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using University.Workshop.Business;
using University.Workshop.Presenters;

namespace University.Workshop.WinApp.Product
{
    public partial class FormProductView : Form
    {
        public static string value;
        SaleVariables variable = SaleVariables.Instance;
        PurchaseVariables variablePurchase = PurchaseVariables.Instance;
        Variables flag = Variables.Instance;
        ReportSaleVariables variableReport = ReportSaleVariables.Instance;
        public FormProductView()
        {
            InitializeComponent();
        }

        private void FormProductView_Load(object sender, EventArgs e)
        {
            this.providerTableAdapter.Fill(this.providerDataSet.Provider);
            this.presentationTableAdapter.Fill(this.presentationDataSet.Presentation);
            if (variableReport.IdProduct == "BrowserProductReportSale")
            {
                tabControlProduct.SelectedTab = tabPageSearchProduct;
                labelSearchType.Visible = false;
                comboBoxSearchType.Visible = false;
                foreach (Control c in this.tabPageSearchProduct.Controls)
                {
                    if (c is Button)
                    {
                             c.Visible = false;
                    }
                }
                    buttonProductSearch.Visible = true;
                    buttonCleanProdcutSearch.Visible = true;
                    buttonCancelSearch.Visible = true;
                    buttonDeleteProduct.Size = new Size(0, 0);
                    buttonHabilityProduct.Size = new Size(0, 0);
            }
            else
            {
                if (flag.Product == "BrowserProductPurchase")
                {
                    tabControlProduct.SelectedTab = tabPageSearchProduct;
                    HideSearchButtons();
                    buttonAddToPurchase.Visible = true;
                }
                else
                {
                    if (flag.Product == "BrowserProductSale")
                    {
                        tabControlProduct.SelectedTab = tabPageSearchProduct;
                        HideSearchButtons();
                        buttonAddToSale.Visible = true;
                    }
                    else
                    {
                        tabControlProduct.SelectedTab = tabPageMainProduct;
                    }
                }
            }
        }
        private void HideSearchButtons()
        {
            buttonHabilityProduct.Visible = false;
            buttonLookProduct.Visible = false;
            buttonEditProduct.Visible = false;
            buttonDeleteProduct.Visible = false;
            checkBoxProductHability.Visible = false;
            buttonCleanProdcutSearch.Visible = false;
            buttonCancelSearch.Visible = false;
        }

        private void buttonRegisterProduct_Click(object sender, EventArgs e)
        {
            tabControlProduct.SelectedTab = tabPageRegisterProduct;
        }

        private void buttonQuitProduct_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAceptProduct_Click(object sender, EventArgs e)
        {
            string code;
            bool control;
            string recet;
            if(radioButtonTrue.Checked)
                recet="si";
            else
                recet="no";
            Model.Product product;
            if (string.IsNullOrEmpty(textBoxCodeProduct.Text) || string.IsNullOrEmpty(textBoxNameProduct.Text))
            {
                    MessageBox.Show(this, "Debe llenar todos los campos ", "Error al llenar datos", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
            }
            else
            {
                code = textBoxCodeProduct.Text;
                control = ProductController.ValidateCode(code);
                if (control == false)
                {
                    textBoxCodeProduct.Clear();
                }
                else
                {
                    product = new Model.Product()
                    {
                        CodeProduct = textBoxCodeProduct.Text,
                        CodeProvider = providerComboBox.SelectedValue.ToString(),
                        DateAdmission = dateTimePickerDateAdmission.Value,
                        NameProduct = textBoxNameProduct.Text,
                        Presentation = presentationComboBox.SelectedValue.ToString(),
                        State = "A",
                        Description=textBoxDescriptionProduct.Text,
                        PrescriptionMedic=recet
                    };
                    ProductController.Register(product);
                    MessageBox.Show(this, "Producto Ingresado", "Registro Satisfactorio", MessageBoxButtons.OK,
                    MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                    ClearAllTextBoxOfRegisterProduct();
                    tabControlProduct.SelectedTab = tabPageMainProduct;
                }

            }
        }

        private void ClearAllTextBoxOfRegisterProduct()
        {
            textBoxCodeProduct.Clear();
            textBoxNameProduct.Clear();
        }

        private void buttonCancelProduct_Click(object sender, EventArgs e)
        {
            ClearAllTextBoxOfRegisterProduct();
            tabControlProduct.SelectedTab = tabPageMainProduct;
        }

        private void ValidateCodeProduct(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar) || char.IsLetter(e.KeyChar) || char.IsControl(e.KeyChar))
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void ValidateNameProduct(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsNumber(e.KeyChar))
                e.Handled = false;
            else
            {

                e.Handled = true;
            }
        }

        private void buttonProductSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCampSearch.Text))
            {
                MessageBox.Show(this, "Debe llenar el campo de busqueda ", "Error al llenar datos", MessageBoxButtons.OK,
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            }
            else
            {

                if (checkBoxProductHability.Checked)
                {
                    buttonHabilityProduct.Visible = true;
                    buttonDeleteProduct.Visible = false;
                    if (comboBoxSearchType.Text == "Codigo Producto")
                    {
                        this.productTableAdapter.FillByCodeNotHability(this.searchDataSet.Product, "%" + textBoxCampSearch.Text + "%");
                    }
                    if (comboBoxSearchType.Text == "Nombre Producto")
                    {
                        this.productTableAdapter.FillByNameNotHability(this.searchDataSet.Product, "%" + textBoxCampSearch.Text + "%");
                    }
                }
                else
                {
                    buttonHabilityProduct.Visible = false;
                    if (flag.Product == "BrowserProductSale" || flag.Product == "BrowserProductPurchase")
                        buttonDeleteProduct.Visible = false;
                    else
                        buttonDeleteProduct.Visible = true;

                    if (comboBoxSearchType.Text == "Codigo Producto")
                    {
                        this.productTableAdapter.FillByCode(this.searchDataSet.Product, "%" + textBoxCampSearch.Text + "%");
                    }
                    if (comboBoxSearchType.Text == "Nombre Producto")
                    {
                        this.productTableAdapter.FillByName(this.searchDataSet.Product, "%" + textBoxCampSearch.Text + "%");
                    }
                }
            }
        }

        private void buttonSearchProduct_Click(object sender, EventArgs e)
        {
            comboBoxSearchType.Text = "Codigo Producto";
            textBoxCampSearch.Text = "";
            this.productTableAdapter.FillByCode(this.searchDataSet.Product, "-");
            buttonHabilityProduct.Visible = false;
            tabControlProduct.SelectedTab = tabPageSearchProduct;
        }

        private void KeyPressSearchType(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void buttonCancelSearch_Click(object sender, EventArgs e)
        {
            if (variableReport.IdProduct == "BrowserProductReportSale") {
                variableReport.IdProduct = string.Empty;
                this.Close();
            }
            else 
            { 
                CleanSearch();
                tabControlProduct.SelectedTab = tabPageMainProduct;
            }
        }

        private void buttonCleanProdcutSearch_Click(object sender, EventArgs e)
        {
            CleanSearch();
        }
        public void CleanSearch()
        {
            this.productTableAdapter.FillByCode(this.searchDataSet.Product, "-");
            buttonHabilityProduct.Visible = false;
            textBoxCampSearch.Text = "";
            checkBoxProductHability.Checked = false;
            comboBoxSearchType.Text = "Codigo Producto";
        }

        private void buttonLookProductAccept_Click(object sender, EventArgs e)
        {
            tabControlProduct.SelectedTab = tabPageSearchProduct;

        }

        private void buttonLookProduct_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = productDataGridView.CurrentRow;
            if (productDataGridView != null)
            {
                if (row == null)
                {
                    MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                }
                else
                {
                    value = (string)row.Cells[0].Value;
                    tabControlProduct.SelectedTab = tabPageLookProduct;
                    DataTable table = new DataTable();
                    table = ProductController.LookProduct(value);

                    textBoxLookCodeProduct.Text = table.Rows[0][0].ToString();
                    textBoxLookNameProduct.Text = table.Rows[0][1].ToString();
                    textBoxLookPresentation.Text = table.Rows[0][2].ToString();
                    textBoxLookProviderProduct.Text = table.Rows[0][3].ToString();
                    textBoxLookAdmissionProduct.Text = table.Rows[0][4].ToString();
                    textBoxLookDescription.Text = table.Rows[0][5].ToString();
                    textBoxLookRecetMedic.Text = table.Rows[0][6].ToString();
                }

            }
            else
            {
                MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            }

        }

        private void buttonEditProduct_Click(object sender, EventArgs e)
        {
            
            DataGridViewRow row = productDataGridView.CurrentRow;
            if (productDataGridView != null)
            {
                if (row == null)
                {
                    MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                }
                else
                {
                    value = (string)row.Cells[0].Value;
                    tabControlProduct.SelectedTab = tabPageEditProduct;
                    DataTable table = new DataTable();
                    table = ProductController.LookProduct(value);

                    textBoxEditCodeProduct.Text = table.Rows[0][0].ToString();
                    textBoxEditNameProduct.Text = table.Rows[0][1].ToString();
                    PresentationEditcomboBox.Text = table.Rows[0][2].ToString();
                    textBoxDescriptionModify.Text = table.Rows[0][5].ToString();
                    ProviderEditcomboBox.Text = table.Rows[0][3].ToString();
                    dateTimePickerEditProduct.Value = DateTime.Parse(table.Rows[0][4].ToString());
                    string prescription = table.Rows[0][6].ToString();
                    if (table.Rows[0][6].ToString() == "si")
                    {
                        radioButtonModifyTrue.Checked = true;
                    }
                    else 
                    {
                        radioButtonModifyFalse.Checked = true;
                    }
                    
                }

            }
            else
            {
                MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            }

        }

        private void buttonEditCancel_Click(object sender, EventArgs e)
        {
            tabControlProduct.SelectedTab = tabPageSearchProduct;
        }

        private void buttonProductEdit_Click(object sender, EventArgs e)
        {
            string res;
            if (radioButtonModifyTrue.Checked)
                res = "si";
            else
                res = "no";
            Model.Product products;
            products = new Model.Product()
            {
                CodeProduct = textBoxEditCodeProduct.Text,
                CodeProvider = ProviderEditcomboBox.SelectedValue.ToString(),
                DateAdmission = dateTimePickerEditProduct.Value,
                NameProduct = textBoxEditNameProduct.Text,
                Presentation = PresentationEditcomboBox.SelectedValue.ToString(),
                Description = textBoxDescriptionModify.Text,
                State = "A",
                PrescriptionMedic = res
            };

            ProductController.EditProduct(products);

            MessageBox.Show(this, "Producto Modificado", "Modificacion Satisfactoria", MessageBoxButtons.OK,
            MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
            MessageBoxOptions.RightAlign);

            CleanSearch();
            tabControlProduct.SelectedTab = tabPageSearchProduct;
        }

        private void buttonDeleteProduct_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = productDataGridView.CurrentRow;
            if (productDataGridView != null)
            {
                if (row == null)
                {
                    MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                }
                else
                {
                    value = (string)row.Cells[0].Value;
                    MessageBox.Show(this, "Producto Eliminado", "Eliminar Producto", MessageBoxButtons.OK,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                    bool controler = ProductController.DeleteProduct(value);
                    CleanSearch();


                }

            }
            else
            {
                MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            }

        }

        private void buttonHabilityProduct_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = productDataGridView.CurrentRow;
            if (productDataGridView != null)
            {
                if (row == null)
                {
                    MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                }
                else
                {
                    value = (string)row.Cells[0].Value;
                    MessageBox.Show(this, "El producto fue habiliatado", "Habilitar Producto", MessageBoxButtons.OK,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                    bool control = ProductController.EnableProduct(value);
                    CleanSearch();
                }

            }
            else
            {
                MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            }

        }

        private void productDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (variableReport.IdProduct == "BrowserProductReportSale")
            {
                variableReport.IdProduct = productDataGridView.Rows[productDataGridView.CurrentRow.Index].Cells[0].Value.ToString();
                this.Close();
            }
            else
                MessageBox.Show(this, "No se selecciono ningun registro!", "Seleccionar Campo", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
        }

        private void buttonAddToSale_Click(object sender, EventArgs e)
        {
            if (productDataGridView.Rows.Count != 0)
            {
                variable.ProductCode = productDataGridView.SelectedRows[0].Cells[0].Value.ToString();
                Close();
            }
            else
                MessageBox.Show(this, "No se selecciono ningun registro!", "Seleccionar Campo", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
        }

        private void FormProductView_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (variableReport.IdProduct == "BrowserProductReportSale")
            {
                variableReport.IdProduct = string.Empty;
            }
        }

        private void buttonAddToPurchase_Click(object sender, EventArgs e)
        {
            if (productDataGridView.Rows.Count != 0)
            {
                variablePurchase.ProductCode = productDataGridView.SelectedRows[0].Cells[0].Value.ToString();
                variablePurchase.ProductName = productDataGridView.SelectedRows[0].Cells[1].Value.ToString();
                Close();
            }
            else
                MessageBox.Show(this, "No se selecciono ningun registro!", "Seleccionar Campo", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
        }
    }
}
        

